<?php
/**
 * 名称：MessPhotoAction
 * 描述：后台管理消息图片
 * 作者：next 时间：2013-11-27
 * 版本号：V1.0.0
 */
class MessPhotoAction extends CommonAction {
	protected $mongo;
	public function _initialize() {
		parent::_initialize();
		import ( "ORG.Mongo.DbMongo" );
		$nosqlConfig = include SITE_PATH . '/apps/api/Conf/nosql.php';
		if(!$nosqlConfig) {
			exit;
		}
		if($nosqlConfig['user']){
			$this->mongo = new DbMongo ( "mongodb://".$nosqlConfig['user'].":".$nosqlConfig['password']."@".$nosqlConfig['host'].":".$nosqlConfig['port'] );
		}else{
			$this->mongo = new DbMongo ( "mongodb://".$nosqlConfig['host'].":".$nosqlConfig['port'] );
		}
		$this->mongo->selectDb ( $nosqlConfig['dbname'] );
		$this->mongo->ensureIndex('picture',array('userid'=>1));
		$this->mongo->ensureIndex('picture',array('messageid'=>1));
		$this->mongo->ensureIndex('picture',array('circleid'=>1));
		$this->mongo->ensureIndex('picture',array('loc'=>'2d'));
		$this->mongo->ensureIndex('picture',array('loc'=>'2dsphere'));
		$this->mongo->ensureIndex('picture',array('datetime'=>1));
	}

	/**
	 * 得到消息图片列表
	 */
	public function messphotolist() {
		import ( "ORG.Util.Page" );
		$messageid = $_GET['messageid'];
		$circleid = $_GET['circleid'];
		$userid = $_GET['userid'];
		$page = empty($_GET['p'])?0:intval($_GET['p']);
		$query_condition = array();
		if (! empty ( $messageid )) {
			$query_condition["messageid"] = intval($messageid);
		}
		if (! empty ( $circleid )) {
			$query_condition["circleid"] = $circleid;
		}
		if (! empty ( $userid )) {
			$query_condition["userid"] = $userid;
		}
		$skip = ($page-1)*$count;
		$skip = $skip < 0 ? 0 : $skip;
		$condition = array('start'=>$skip,'limit'=>20);		
		//列表
		$list = $this->mongo->find('picture',$query_condition,$condition);		
		//总笔数
		$count = $this->mongo->find("picture",$query_condition).count();
		$p = new Page ( $count, 20 );
		$page = $p->show ();
		$this->assign ( 'page', $page );
		$this->assign ( $_GET );
		foreach ($list as $k => $v){
			$datetimes = explode(" ", $v['datetime']);
			$list[$k]['datetime'] = common('Date')->toDate($datetimes[1]);
		}
		$this->assign ( 'list', $list );
		$this->display ();
	}
	
	/**
	 * 删除消息图片
	 */
	public function deleteMessPhotoFile(){
		if ( empty($_POST['ids']) ) return false;
		$idsarray = explode ( ',', $_POST ['ids'] );
		$mongoIds = array();
		for($i = 0; $i < count($idsarray); $i++) {
			$mongoIds[] = new MongoId($idsarray[$i]);
		}
		$condition = array("_id" => array('$in' => $mongoIds));
		$options = array('safe'=>true);
		if($this->mongo->remove('picture',$condition,$options)) {
			echo 1;
			return;
		}else{
			echo 0;
			return;
		}		
	}
}